package SpringCode10Affair.SpringCode10Affair.service.impl;

import java.util.List;

import javax.sql.DataSource;

import org.springframework.jdbc.core.JdbcTemplate;

import SpringCode10Affair.SpringCode10Affair.bean.StudentsBean;
import SpringCode10Affair.SpringCode10Affair.bean.SutdentsRowMapper;
import SpringCode10Affair.SpringCode10Affair.service.IStudentService;

public class StudentServiceImpl implements IStudentService{

	private JdbcTemplate jdbc;
	
	public void setDataSource(DataSource ds) {
		this.jdbc = new JdbcTemplate(ds);
	}
	
	@Override
	public void save(StudentsBean bean) {
		jdbc.update("Insert into students ( stu_name , stu_age , stu_gender ) values (?,?,?)" ,
				new Object[] {bean.getName(),bean.getAge(),bean.getGengder() },
				new int[] {java.sql.Types.VARCHAR,java.sql.Types.INTEGER,java.sql.Types.INTEGER} );
		// 默认 spring 事务 对 RuntimeException 进行回滚。
		throw new RuntimeException("测试事务异常！");
	}

	@Override
	public List<StudentsBean> findAll() {
		List<StudentsBean> li = jdbc.query("select * from students", new SutdentsRowMapper());
		return li;
	}

}
